﻿using M.ShopMachine.Write.WebApi.Application.Command.DeviceCommand;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.ShopMachine.Write.WebApi.Controllers
{
    /// <summary>
    /// 机床api
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        private readonly IMediator mediator;

        public DeviceController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        #region 添加

        /// <summary>
        /// 机床-添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDeviceAsync(AddDeviceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        #endregion

        #region 修改

        /// <summary>
        /// 修改机床信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdDeviceAsync(UpdDeviceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 修改车间添加机床信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdShopDeviceAsync(UpdShopDeviceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 修改机床状态（停工/恢复）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateDeviceAsync(UpdStateDeviceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code =500;
                result.Message = ex.Message;
                throw;
            }
        }

        #endregion
    }
}
